#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "tile.h"

#include <QImage>
#include <QLabel>
#include <QDebug>
#include <QDir>
#include <QPainter>

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  ui(new Ui::MainWindow)
{
  ui->setupUi(this);
  ui->wallSpin->setValue(TileMap::DELTA_WALL);
  ui->tileSpin->setValue(TileMap::DELTA_TILE);
  connect(ui->wallSpin,SIGNAL(valueChanged(int)),this,SLOT(parseImages()));
  connect(ui->tileSpin,SIGNAL(valueChanged(int)),this,SLOT(parseImages()));

  parseImages();
}

MainWindow::~MainWindow()
{
  delete ui;
}

void MainWindow::parseImages()
{
  TileMap::DELTA_WALL = ui->wallSpin->value();
  TileMap::DELTA_TILE = ui->tileSpin->value();

  QFile("D:/output.png").remove();
  QFileInfoList entries = QDir("D:/").entryInfoList(QStringList() << "*.png" << "*.jpg");
  QImage result(1024*2,768 * (entries.size() / 2 + 1),QImage::Format_ARGB32);
  result.fill(Qt::white);
  QPainter p(&result);
  p.setPen(Qt::red);
  p.setBrush(Qt::transparent);

  int x=0,y=0;

  foreach(const QFileInfo& info, entries)
  {
    QImage img(info.absoluteFilePath());
    TileMap map(200,200,30,img);
    map.highlightWalls(img);
    map.highlightExits(img);
    p.drawImage(x,y,img.scaled(1024,768,Qt::KeepAspectRatio));
    p.drawRect(x,y,1024,768);

    if (x>0)
    {
      x=0;
      y+=768;
    }
    else
    {
      x+=1024;
    }
  }

  result.save("D:/output.png");
  ui->label->setPixmap(QPixmap::fromImage(result));
}
